﻿using System;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace WebsiteLoadTool.Configuration
{
    public class ConfigurationWindowViewModel : NotificationObject
    {
        private DelegateCommand okCommand;

        private DelegateCommand newWebsiteCommand;
        private DelegateCommand deleteWebsiteCommand;

        private string logPath;
        private bool useLog;
        private bool startWithApplication;
        private int interval;
        private int maxResultLength;

        private ObservableCollection<Website> websites;
        private Website selectedWebsite;

        public event EventHandler OnOK;


        public ConfigurationWindowViewModel()
        {
            OKCommand = new DelegateCommand(OK);

            NewWebsiteCommand = new DelegateCommand(NewWebsite, CanNewWebsite);
            DeleteWebsiteCommand = new DelegateCommand(DeleteWebsite, CanDeleteWebsite);

            Websites = DataFileHelper.LoadWebsites();

            UseLog = bool.Parse(ConfigSettingHelper.ReadSetting("UseLog"));
            LogPath = ConfigSettingHelper.ReadSetting("LogPath");
            Interval = int.Parse(ConfigSettingHelper.ReadSetting("Interval"));
            StartWithApplication = bool.Parse(ConfigSettingHelper.ReadSetting("StartWithApplication"));
            MaxResultLength = int.Parse(ConfigSettingHelper.ReadSetting("MaxResultLength"));
        }


        public ObservableCollection<Website> Websites
        {
            get { return websites; }
            set
            {
                if (!ObservableCollection<Website>.Equals(websites, value))
                {
                    websites = value;
                    RaisePropertyChanged("Websites", "CanNewWebsite", "CanEditWebsite", "CanDeleteWebsite");
                }
            }
        }

        public Website SelectedWebsite
        {
            get { return selectedWebsite; }
            set
            {
                if (!Website.Equals(selectedWebsite, value))
                {
                    selectedWebsite = value;
                    RaisePropertyChanged("SelectedWebsite");

                    NewWebsiteCommand.RaiseCanExecuteChanged();
                    DeleteWebsiteCommand.RaiseCanExecuteChanged();
                }
            }
        }


        public DelegateCommand OKCommand
        {
            get { return okCommand; }
            set
            {
                if (!DelegateCommand.Equals(okCommand, value))
                {
                    okCommand = value;
                    RaisePropertyChanged("OKCommand");
                }
            }
        }

        public DelegateCommand NewWebsiteCommand
        {
            get { return newWebsiteCommand; }
            set
            {
                if (!DelegateCommand.Equals(newWebsiteCommand, value))
                {
                    newWebsiteCommand = value;
                    RaisePropertyChanged("NewWebsiteCommand");
                }
            }
        }

        public DelegateCommand DeleteWebsiteCommand
        {
            get { return deleteWebsiteCommand; }
            set
            {
                if (!DelegateCommand.Equals(deleteWebsiteCommand, value))
                {
                    deleteWebsiteCommand = value;
                    RaisePropertyChanged("DeleteWebsiteCommand");
                }
            }
        }

        public string LogPath
        {
            get { return logPath; }
            set
            {
                if (!string.Equals(logPath, value))
                {
                    logPath = value;
                    RaisePropertyChanged("LogPath");
                }
            }
        }

        public bool UseLog
        {
            get { return useLog; }
            set
            {
                if (!bool.Equals(useLog, value))
                {
                    useLog = value;
                    RaisePropertyChanged("UseLog");
                }
            }
        }

        public bool StartWithApplication
        {
            get { return startWithApplication; }
            set
            {
                if (!bool.Equals(startWithApplication, value))
                {
                    startWithApplication = value;
                    RaisePropertyChanged("StartWithApplication");
                }
            }
        }

        public int Interval
        {
            get { return interval; }
            set
            {
                if (!int.Equals(interval, value))
                {
                    interval = value;
                    RaisePropertyChanged("Interval");
                }
            }
        }

        public int MaxResultLength
        {
            get { return maxResultLength; }
            set
            {
                if (!int.Equals(maxResultLength, value))
                {
                    maxResultLength = value;
                    RaisePropertyChanged("MaxResultLength");
                }
            }
        }




        private void OK()
        {
            ConfigSettingHelper.WriteSetting("UseLog", UseLog.ToString());
            ConfigSettingHelper.WriteSetting("LogPath", LogPath);
            ConfigSettingHelper.WriteSetting("Interval", Interval.ToString());
            ConfigSettingHelper.WriteSetting("StartWithApplication", StartWithApplication.ToString());
            ConfigSettingHelper.WriteSetting("MaxResultLength", MaxResultLength.ToString());

            DataFileHelper.WriteWebsites(Websites);

            if (OnOK != null)
                OnOK(this, new EventArgs());
        }


        private bool CanNewWebsite()
        {
            return (Websites != null);
        }

        private void NewWebsite()
        {
            Websites.Add(new Website() { ID = Guid.NewGuid() });
        }

        private bool CanDeleteWebsite()
        {
            return (Websites != null && SelectedWebsite != null);
        }

        private void DeleteWebsite()
        {
            Websites.Remove(SelectedWebsite);
        }
    }
}
